//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';
$.jgrid.defaults.height='auto';

//工具集合Tools
window.T = {};
var url = function(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
};
// 获取请求参数
// 使用示例
// location.href = http://localhost/index.html?id=123
// T.p('id') --> 123;
T.p = url;


//登录token
var token = localStorage.getItem("token");

/*if(token == 'null'){
    parent.location.href = '/views/login.html';
    //parent.location.href = '/login.html';
}*/


//jqgrid全局配置
$.extend($.jgrid.defaults, {
    ajaxGridOptions : {
        headers: {
            "token": token
        }
    }
});

//权限判断
function hasPermission(permission) {
    if (window.parent.permissions.indexOf(permission) > -1) {
        return true;
    } else {
        return false;
    }
}

//重写alert
window.alert = function(msg, callback){
    parent.layer.alert(msg, function(index){
        parent.layer.close(index);
        if(typeof(callback) === "function"){
            callback("ok");
        }
    });
}

//重写confirm式样框
window.confirm = function(msg, callback){
    parent.layer.confirm(msg, {btn: ['确定','取消']},
        function(){//确定事件
            if(typeof(callback) === "function"){
                callback("ok");
            }
        });
}

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
        alert("请选择一条记录");
        return ;
    }

    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
        alert("只能选择一条记录");
        return ;
    }

    return selectedIDs[0];
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
        alert("请选择一条记录");
        return ;
    }

    return grid.getGridParam("selarrrow");
}

/**
 * 根据参数名获取选中记录的某一字段值
 * @param key
 * @param gridId
 */
function getValueBySelectedRowsKey(key,gridId) {
    var grid;
    if(typeof name === "undefined" || !(gridId=$.trim(gridId))) {
        grid = $("#jqGrid");
    } else {
        grid=$(gridId);
    }
    var selectedId = grid.jqGrid('getGridParam','selrow');//获得所选行的id
    var rowData = grid.jqGrid("getRowData",selectedId);//根据id获得本行的所有数据
    var v= rowData[key]; //根据colModel的name即参数key，获得制定列的值
    return v;
}

//判断是否为空
function isBlank(value) {
    return !value || !/\S/.test(value)
}

function getUUID() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);

    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}


/*if(!validToken()){
    //parent.location.href = '/login.html';
    // localStorage.removeItem("token");
    //parent.location.href = '/login.html';
}*/


/*
validToken();*/

/*设置jqGrid表格序号列的宽度*/
function setFirstColumWidth()
{
    if(("#jqGrid")!=null)
    {
        $("#jqGrid").find("td:eq(0)").css("width",60);
        $("#gbox_jqGrid").find("th:eq(0)").css("width",60);
    }
}





